const path = require("path");
const express = require("express");
const axios = require("axios");
const data = require("./data");
const app = express();
app.use(express.urlencoded({
    extended:false
}))
app.use(express.json());
// app.use((req,res,next)=>{
//     res.set("Access-Control-Allow-Origin","*");
//     next();
// })
app.use(express.static(path.resolve(__dirname,"../")))
// /api/xhr
app.use("/api/:url",(req,res)=>{
    axios.get("http://127.0.0.1:8083/"+req.params.url).then(value=>{
        res.json(value.data);
    })
    // console.log(req.params.url)
    // res.json({
    //     ok:1
    // })
})
app.get("/xhr",(req,res)=>{
    res.json(data);
});
app.get("/xhr2",(req,res)=>{
    res.send(data.scoreList.map(item=>(`
         <div>
            <p>用户名：${item.userName}</p>
            <p>年龄：${item.age}</p>
            <p>性别：${item.sex}</p>
            <p>成绩：${JSON.stringify(item.score)}</p>
            <hr/>
        </div>
    `)).join(""))
});
app.post("/xhr",(req,res)=>{
    console.log("query",req.query);
    console.log("body",req.body);
    res.json({
        ok:1,
        msg:"post->success"
    })
})
app.listen(8082,()=>{
    console.log("success");
})